package com.yalantis.ucrop.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.RectF;
import android.support.annotation.NonNull;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.yalantis.ucrop.R;
import com.yalantis.ucrop.callback.CropBoundsChangeListener;
import com.yalantis.ucrop.callback.OverlayViewChangeListener;

public class UCropView extends FrameLayout {

    private GestureCropImageView mGestureCropImageView;
    private final OverlayView mViewOverlay;
    private float mRatio;
    private boolean mIsAnswerCrop;

    public UCropView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public UCropView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        LayoutInflater.from(context).inflate(R.layout.ucrop_view, this, true);

//        if(mIsAnswerCrop){
//            LayoutInflater.from(context).inflate(R.layout.ucrop_view, this, true);
//        }else{
//            LayoutInflater.from(context).inflate(R.layout.ucrop_view_icon, this, true);
//        }
        mGestureCropImageView = (GestureCropImageView) findViewById(R.id.image_view_crop);
        mViewOverlay = (OverlayView) findViewById(R.id.view_overlay);

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ucrop_UCropView);
        mViewOverlay.processStyledAttributes(a);
        mGestureCropImageView.processStyledAttributes(a);
        a.recycle();
        setListenersToViews();
    }


    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        resetImageAndOverLayView();
    }

    /**
     * @date 2017/12/8
     * @author kb_jay
     * @Desctiption 客制化，获取图片的宽高比，获取view的高度，动态设置view的宽度，目的是为了可以显示全图片
     */
    private void resetImageAndOverLayView() {
        int width = (int) ((mViewOverlay.getMeasuredHeight() - mViewOverlay.getPaddingBottom()
                - mViewOverlay.getPaddingTop()) * mRatio + 0.5f);
        ViewGroup.LayoutParams lp = mViewOverlay.getLayoutParams();
        lp.width = width;
        mViewOverlay.setLayoutParams(lp);

        ViewGroup.LayoutParams lpImg = mGestureCropImageView.getLayoutParams();
        lpImg.width = width;
        mGestureCropImageView.setLayoutParams(lpImg);
    }

    /**
     * @date 2017/12/8
     * @author kb_jay
     * @Desctiption 获取原图片的宽高比
     */
    public void setAspectRatio(float ratio) {
        mRatio = ratio;
    }

    /**
     * @date 2017/12/8
     * @author kb_jay
     * @Desctiption 客制化
     */
    public void setIsAnserCrop(boolean isCustom) {
        mIsAnswerCrop = isCustom;
    }

    private void setListenersToViews() {
        mGestureCropImageView.setCropBoundsChangeListener(new CropBoundsChangeListener() {
            @Override
            public void onCropAspectRatioChanged(float cropRatio) {
                mViewOverlay.setTargetAspectRatio(cropRatio);
            }
        });
        mViewOverlay.setOverlayViewChangeListener(new OverlayViewChangeListener() {
            @Override
            public void onCropRectUpdated(RectF cropRect) {
                mGestureCropImageView.setCropRect(cropRect);
            }
        });
    }

    @Override
    public boolean shouldDelayChildPressedState() {
        return false;
    }

    @NonNull
    public GestureCropImageView getCropImageView() {
        return mGestureCropImageView;
    }

    @NonNull
    public OverlayView getOverlayView() {
        return mViewOverlay;
    }

    /**
     * Method for reset state for UCropImageView such as rotation, scale, translation.
     * Be careful: this method recreate UCropImageView instance and reattach it to layout.
     */
    public void resetCropImageView() {
        removeView(mGestureCropImageView);
        mGestureCropImageView = new GestureCropImageView(getContext());
        setListenersToViews();
        mGestureCropImageView.setCropRect(getOverlayView().getCropViewRect());
        addView(mGestureCropImageView, 0);
    }
}